/**
 * 
 */
package com.easidea.tim.dao.inventory;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springside.core.dao.extend.HibernateEntityExtendDao;

import com.easidea.tim.entity.inventory.Inventory;

/**
 * @author Harrison
 *
 */
public class InventoryDao extends HibernateEntityExtendDao<Inventory> {

	public Boolean deleteInventory(Long id) {
		Boolean rst = false;
		if(id != null){
			Inventory ti = get(id);
			if(ti != null){
				getHibernateTemplate().delete(ti);
				rst = true;
			}
		}
		return rst;
	}
	

	/**
	 * 盘点当前产品ID是否有相应的盘点记录
	 * @param productId
	 * @return
	 */
	public boolean isInventoryThisProductNow(Long productId, boolean isSample){
		boolean b = false;
		Criteria cte = this.createUnDeletedCriteria();
		cte.add(Restrictions.eq("product.id", productId));
		cte.add(Restrictions.ne("status", Inventory.INVENTORY_STATUS_COMPLETE));
		cte.add(Restrictions.eq("sampleFlag",isSample));
		cte.setProjection(Projections.rowCount());
		Integer rowCounts = (Integer)cte.uniqueResult();
		if(rowCounts != null && rowCounts.intValue() > 0) b = true;
		return b;
	}

}
